#include "ULUI_LookupSingle.h"
#include "ULUI_LookupRange.h"
#include "ULUI_LookupLatest.h"
#include "ULUI_LookupLatestBlue.h"
#include "ULUI_HistoryLookup.h"
#include "UL_Global.h"

#define HISTORY_MENU_START_X	100
#define HISTORY_MENU_START_Y	100

ULUI_HistoryLookup ULUI_HistoryLookup::historyLookup;

enum HistoryLookupIDs{
	LOOKUP_SINGLE,
	LOOKUP_RANGE,
	LOOKUP_LATESTN,
	LOOKUP_LATESTN_BLUE,
	EXIT	
};	

static char *menuTitles[] = {
	"单期查询", //By serial number or date
	"范围查询",
	"最近期数查询",
	"特定蓝球查询",
	"返回"
};

void ULUI_HistoryLookup::Init()
{
	int i;
	ULUI_Menu menu;

	AttachEventHandler(this);

	menu.SetFont(UL_Global::UnionLottoFont());
	selectedMenu = -1;

	for (i = 0; i <= EXIT; i++){
		menu.SetID(i);
		menu.SetTitle(menuTitles[i]);
		menus.AddMenu(menu);
	}

	menus.SetFirstMenuXY(VSDL_Point(HISTORY_MENU_START_X, HISTORY_MENU_START_Y));

}

void ULUI_HistoryLookup::CleanUp()
{
	menus.Reset();
}

void ULUI_HistoryLookup::Pause()
{
}

void ULUI_HistoryLookup::Resume()
{
	selectedMenu = -1;
}

void ULUI_HistoryLookup::Update(VSDL_GameEngine *game)
{
	if (selectedMenu == -1){
		return ;
	}

	switch (selectedMenu){
		case LOOKUP_SINGLE:
			game->PushState(ULUI_LookupSingle::Instance());
			break;
		case LOOKUP_RANGE:
			game->PushState(ULUI_LookupRange::Instance());
			break;
		case LOOKUP_LATESTN:
			game->PushState(ULUI_LookupLatest::Instance());
			break;
		case LOOKUP_LATESTN_BLUE:
			game->PushState(ULUI_LookupLatestBlue::Instance());
			break;
		case EXIT:
			game->PopState();
			break;
	}
}

void ULUI_HistoryLookup::Draw(VSDL_GameEngine *game)
{
	UL_Global::UnionLottoShowBg();
	UL_Global::UnionLottoShowTitle();
	menus.Show(game->GetSurface());
}

ULUI_HistoryLookup *ULUI_HistoryLookup::Instance()
{
	return &historyLookup;
}


void __HANDLE_EVENT ULUI_HistoryLookup::OnKeyDown(SDLKey keysym, unsigned char *keyStates, void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	if (keyStates[SDLK_ESCAPE]){
		game->PopState();
	}
}

void __HANDLE_EVENT ULUI_HistoryLookup::OnMouseMove(int button, unsigned char buttonState, int absX, int absY, int relX, int relY, void *arg)
{
	menus.UpdateMenuList(absX, absY);
}

void __HANDLE_EVENT ULUI_HistoryLookup::OnMouseDown(int button, unsigned char buttonState, int absX, int absY, void *arg)
{
	int i;
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	selectedMenu = -1;

	for (i = 0; i <= EXIT; i++){
		ULUI_Menu &menu = menus.GetMenu(i);

		if (menu.IsXYInRect(absX, absY)){
			selectedMenu = i;
			break;
		}
	}
	
}

void __HANDLE_EVENT ULUI_HistoryLookup::OnQuit(void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);
}
